package utils

import (
	kcode "longblog/work/code"
	"fmt"
	"github.com/gin-gonic/gin"
	kcommon "longblog/framework/tools/common"
	"io/ioutil"
	"mime/multipart"
	"os"
	"path"
	"strconv"
	"strings"
	"time"
)

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

func SaveFile(c *gin.Context, dir string, fileHeader *multipart.FileHeader) (error, int, string, string) {
	is, err := PathExists(dir)
	if err != nil {
		return err, kcode.FILE_UPLOAD_FAIL, "", ""
	}
	if !is {
		_ = os.MkdirAll(dir, 755)
	}
	randNum := kcommon.GetRandomString(6, 0)
	fileExt := path.Ext(fileHeader.Filename)
	times := strconv.FormatInt(time.Now().UnixNano(), 10)
	savePath := fmt.Sprintf("%s%s%s", times, randNum, fileExt)
	if err := c.SaveUploadedFile(fileHeader, fmt.Sprint(dir, savePath)); err != nil {
		return err, kcode.FILE_UPLOAD_FAIL, "", ""
	}
	return nil, kcode.SUCCESS_STATUS, savePath, fileExt
}

func SaveBurstFile(c *gin.Context, dir string, index int64, fileExt string, fileHeader *multipart.FileHeader) (error, int, string, string) {
	is, err := PathExists(dir)
	if err != nil {
		return err, kcode.FILE_UPLOAD_FAIL, "", ""
	}
	if !is {
		_ = os.MkdirAll(dir, 755)
	}
	savePath := fmt.Sprintf("%s/%d%s", dir, index, fileExt)
	if err := c.SaveUploadedFile(fileHeader, savePath); err != nil {
		return err, kcode.FILE_UPLOAD_FAIL, "", ""
	}
	return nil, kcode.SUCCESS_STATUS, savePath, fileExt
}

func AssertFloat64(i interface{}) float64 {
	if v, ok := i.(float64); ok {
		return v
	}
	return 0
}
func AssertString(i interface{}) string {
	if v, ok := i.(string); ok {
		return v
	}
	return ""
}
func AssertInt64(i interface{}) int64 {
	if v, ok := i.(int64); ok {
		return int64(v)
	}
	return 0
}
func AssertBool(i interface{}) bool {
	if v, ok := i.(bool); ok {
		return v
	}
	return false
}

func GetCurrentPath() (string, error) {
	dir, err := os.Getwd()
	if err != nil {
		return "", err
	}
	return strings.Replace(dir, "\\", "/", -1), nil
}

func FormatTime(timeStr string) string {
	formatTime, err := time.ParseInLocation("2006-01-02T15:04:05+08:00", timeStr, time.Local)
	if err != nil {
		return timeStr
	}
	return time.Unix(formatTime.Unix(), 0).Format("2006-01-02 15:04:05")
}

func GetOneDirFileNum(path string) (int64, []string, error) {
	var count int64
	fileName := make([]string, 0)
	files, err := ioutil.ReadDir(path)
	if err != nil {
		return count, fileName, err
	}
	for _, f := range files {
		fileName = append(fileName, f.Name())
		count++
	}
	return count, fileName, nil
}

func MergeFile(outPutFile string, path string, length int, suffix string) error {
	tmpFile, err := os.OpenFile(outPutFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
	defer func() {
		_ = tmpFile.Close()
	}()
	if err != nil {
		return err
	}
	for i := 1; i <= length; i++ {
		f, err := os.OpenFile(fmt.Sprintf("%s/%d%s", path, i, suffix), os.O_RDONLY, os.ModePerm)
		if err != nil {
			return err
		}
		blob, err := ioutil.ReadAll(f)
		if err != nil {
			return err
		}
		if _, err := tmpFile.Write(blob); err != nil {
			return err
		}
		_ = f.Close()
	}
	return nil
}
